#coding=utf-8
from flask import Flask
from flask import request
from flask import url_for, redirect, render_template

app = Flask(__name__)

@app.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        print('ljtljt')
        return redirect(url_for('test', id=request.form['nm']))
        #return 'hello name:{} pass:{}'.format(request.form['nm'], request.form['password'])

@app.route('/', methods=['GET', 'POST'])
def home():
    return '''<html>
          <body>

          <form action = "http://localhost:5000/login/" method = "post">
             <p>Enter Name:</p>
             <p><input type = "text" name = "nm" />input your name</p>
             <p><input type = "text" name = "password" />input your password!</p>
             <p><input type = "submit" value = "submit" /></p>
          </form>

          </body>
       </html>
       '''

@app.route('/test/<string:id>', methods=['GET'])
def test(id):
    return 'hello, your id:{}'.format(id)


@app.route('/test_redirect/<name>', methods=['GET', 'POST'])
def test_redirect(name):
    return redirect(url_for('home', name=name))


@app.route('/signin/', methods=['GET'])
def signin_form():
    return '''<form action="/signin" method="post">
              <p><input name="username"></p>
              <p><input name="password" type="password"></p>
              <p><button type="submit">Sign In</button></p>
              </form>'''

@app.route('/track/<log>', methods=['GET', 'POST'])
def trace(log):
    return 'track:{}'.format(log)


@app.route('/signin/', methods=['GET', 'POST'])
def signin():
    # 需要从request对象读取表单内容：
    if request.form['username']=='admin' and request.form['password']=='password':
        return '<h3>Hello, admin!</h3>'
    return '<h3>Bad username or password. {}</h3>'.format(name)


@app.route('/index/')
def index():
    # 往模板中传入的数据
    my_str = 'Hello Word'
    my_int = 10
    my_array = [3, 4, 2, 1, 7, 9]
    my_dict = {
        'name': 'xiaoming',
        'age': 18
    }
    return render_template('hello.html',
                           my_str=my_str,
                           my_int=my_int,
                           my_array=my_array,
                           my_dict=my_dict
                           )


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)
